﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PCR.Logic
{
    using PCR.Base;
      
    /// <summary>
    /// Access the object by XML file
    /// </summary>
    internal class XmlDatabase : IDatabase
    {
        System.Xml.XmlDocument _doc;
        string _configFilePath = string.Empty;

        IObjectAccess<IPatientReport> _patientReportAccess;
        IVirusTypeAccess _virusTypeAccess;
        IStdCurveAccess _stdCurveAccess;
        IPrivilegeAccess _privilegeAccess;
        IConfigAccess _configAccess;

        /// <summary>
        /// Default constructor, it will open the "PCR_Config.xml"
        /// automatically
        /// </summary>
        public XmlDatabase()
        {
            // Open the xml, create new one if not exist
            if (!Initialize())
            {
                System.Diagnostics.Debug.Assert(false,
                    "Initialize XmlDatabase failed!");
            }
            
            // Create instance for each object access
            this._patientReportAccess = new XmlPatientReport(this._doc, this._configFilePath);
            this._virusTypeAccess = new XmlVirusType(this._doc, this._configFilePath);
            this._stdCurveAccess = new XmlStdCurve(this._doc, this._configFilePath);
            this._privilegeAccess = new XmlPrivilege(this._doc, this._configFilePath);
            this._configAccess = new XmlConfig(this._doc, this._configFilePath);
        }

        /// <summary>
        /// Initialize the xml database, open or create one if not exist
        /// </summary>
        /// <returns>true if successfully, false if failed</returns>
        protected bool Initialize()
        {
            this._doc = new System.Xml.XmlDocument();
            this._configFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "PCR_Config.xml";
            if (System.IO.File.Exists(this._configFilePath))
            {
                this._doc.Load(this._configFilePath);
            }
            else
            {
                System.Xml.XmlNode decl = this._doc.CreateNode(System.Xml.XmlNodeType.XmlDeclaration, "", "");
                
                //System.Xml.XmlAttribute encodingAttr = this._doc.CreateAttribute("encoding");
                //encodingAttr.Value = "utf-8";
                //decl.Attributes.Append(encodingAttr);
                
                this._doc.AppendChild(decl);
                this._doc.AppendChild(this._doc.CreateElement("PCRConfig"));
                this._doc.Save(this._configFilePath);
                this._doc.Load(this._configFilePath);
            }

            return true;
        }

        /// <summary>
        /// Get the standard curve object access interface
        /// </summary>
        public IStdCurveAccess StdCurves 
        { 
            get { return this._stdCurveAccess; }
        }

        /// <summary>
        /// Get the patient report interface
        /// </summary>
        public IObjectAccess<IPatientReport> PatientReports 
        {
            get { return this._patientReportAccess; }
        }

        /// <summary>
        /// Get the virus type interface
        /// </summary>
        public IVirusTypeAccess VirusTypes 
        {
            get { return this._virusTypeAccess; }
        }

        /// <summary>
        /// Get the privilege interface
        /// </summary>
        public IPrivilegeAccess Privilege 
        {
            get { return this._privilegeAccess; } 
        }

        /// <summary>
        /// Get the configuration interface
        /// </summary>
        public IConfigAccess Config 
        {
            get { return this._configAccess; } 
        }
    }
}
